import * as mongoose from 'mongoose';

import fastFormatTimeSchema from './base';
import autoPopulateSubs from './plugins/auto-populate-subs';

const { Schema } = mongoose;

const dictionaryTypeSchema = new Schema(
  {
    code: Schema.Types.String,
    name: Schema.Types.String,
    creator: {
      type: Schema.Types.ObjectId,
      ref: 'user',
    },
    ...fastFormatTimeSchema(),
  },
  {
    versionKey: false,
    timestamps: true,
    toJSON: {
      getters: true,
      transform(doc, ret) {
        ret.id = ret._id;
        delete ret._id;
      },
    },
    toObject: {
      getters: true,
      transform(doc, ret) {
        ret.id = ret._id;
        delete ret._id;
      },
    },
  },
);

export default autoPopulateSubs({
  schema: dictionaryTypeSchema,
  methods: ['find', 'findOne'],
  populates: [
    {
      path: 'creator',
      select: 'id username nickName',
    },
  ],
});
